// $Id$
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Globalization;
using Salamanca.DataAccess;
using Salamanca.DataAccess.Collections;
using Salamanca.DataAccess.Data;

namespace Northwind.DomainModel.Data
{

    /// <summary />
    public partial class CustomerMapper
    {

        /// <summary />
        static CustomerMapper()
        {
            Resources.Sql.Culture=CultureInfo.InvariantCulture;
        }

        /// <summary />
        private DomainModelKeyedCollection<Customer> _FindAll(DataMapperCollection dataMappers)
        {
            IDbCommand[] commands=new IDbCommand[1];

            commands[0]=Connection.CreateCommand();
            commands[0].CommandType=CommandType.Text;
            commands[0].CommandText=Resources.Sql.CustomerSelectAll;

            return Load(commands, dataMappers);
        }

        private Customer.DataTransfer _GetDataTransferObject(IList<IDataRecord> records)
        {
            Customer.DataTransfer data=new Customer.DataTransfer();

            return FillData(data, records[0]);
        }

        /// <summary />
        internal static Customer.DataTransfer FillData(Customer.DataTransfer data, IDataRecord record)
        {
            int ord=record.GetOrdinal("CustomerID");
            if (!record.IsDBNull(ord))
                data.CustomerID=record.GetString(ord);

            ord=record.GetOrdinal("CompanyName");
            if (!record.IsDBNull(ord))
                data.CompanyName=record.GetString(ord);

            ord=record.GetOrdinal("ContactName");
            if (!record.IsDBNull(ord))
                data.ContactName=record.GetString(ord);

            ord=record.GetOrdinal("ContactTitle");
            if (!record.IsDBNull(ord))
                data.ContactTitle=record.GetString(ord);

            ord=record.GetOrdinal("Address");
            if (!record.IsDBNull(ord))
                data.Address=record.GetString(ord);

            ord=record.GetOrdinal("City");
            if (!record.IsDBNull(ord))
                data.City=record.GetString(ord);

            ord=record.GetOrdinal("Region");
            if (!record.IsDBNull(ord))
                data.Region=record.GetString(ord);

            ord=record.GetOrdinal("PostalCode");
            if (!record.IsDBNull(ord))
                data.PostalCode=record.GetString(ord);

            ord=record.GetOrdinal("Country");
            if (!record.IsDBNull(ord))
                data.Country=record.GetString(ord);

            ord=record.GetOrdinal("Phone");
            if (!record.IsDBNull(ord))
                data.Phone=record.GetString(ord);

            ord=record.GetOrdinal("Fax");
            if (!record.IsDBNull(ord))
                data.Fax=record.GetString(ord);

            return data;
        }

        /// <summary />
        private static IDbCommand _CreateSelectCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Customer.PrimaryKey key)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CustomerSelect,
                parameterBuilder.GetMarker("CustomerID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 1);
            p.DbType=DbType.String;
            p.Value=key.Value;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateDeleteCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CustomerDelete,
                parameterBuilder.GetMarker("CustomerID", 1)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 1);
            p.DbType=DbType.String;
            p.Value=data.CustomerID;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateInsertCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CustomerInsert,
                parameterBuilder.GetMarker("CustomerID", 1),
                parameterBuilder.GetMarker("CompanyName", 2),
                parameterBuilder.GetMarker("ContactName", 3),
                parameterBuilder.GetMarker("ContactTitle", 4),
                parameterBuilder.GetMarker("Address", 5),
                parameterBuilder.GetMarker("City", 6),
                parameterBuilder.GetMarker("Region", 7),
                parameterBuilder.GetMarker("PostalCode", 8),
                parameterBuilder.GetMarker("Country", 9),
                parameterBuilder.GetMarker("Phone", 10),
                parameterBuilder.GetMarker("Fax", 11)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 1);
            p.DbType=DbType.String;
            p.Value=data.CustomerID;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CompanyName", 2);
            p.DbType=DbType.String;
            p.Value=data.CompanyName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ContactName", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.ContactName ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ContactTitle", 4);
            p.DbType=DbType.String;
            p.Value=(object)data.ContactTitle ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Address", 5);
            p.DbType=DbType.String;
            p.Value=(object)data.Address ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("City", 6);
            p.DbType=DbType.String;
            p.Value=(object)data.City ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Region", 7);
            p.DbType=DbType.String;
            p.Value=(object)data.Region ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("PostalCode", 8);
            p.DbType=DbType.String;
            p.Value=(object)data.PostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Country", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.Country ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Phone", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.Phone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Fax", 11);
            p.DbType=DbType.String;
            p.Value=(object)data.Fax ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary />
        private static IDbCommand _CreateUpdateCommand(IDbConnection connection, ParameterBuilder parameterBuilder, Customer.DataTransfer data)
        {
            IDbCommand command=connection.CreateCommand();
            command.CommandType=CommandType.Text;
            command.CommandText=string.Format(
                CultureInfo.InvariantCulture,
                Resources.Sql.CustomerUpdate,
                parameterBuilder.GetMarker("CompanyName", 1),
                parameterBuilder.GetMarker("ContactName", 2),
                parameterBuilder.GetMarker("ContactTitle", 3),
                parameterBuilder.GetMarker("Address", 4),
                parameterBuilder.GetMarker("City", 5),
                parameterBuilder.GetMarker("Region", 6),
                parameterBuilder.GetMarker("PostalCode", 7),
                parameterBuilder.GetMarker("Country", 8),
                parameterBuilder.GetMarker("Phone", 9),
                parameterBuilder.GetMarker("Fax", 10),
                parameterBuilder.GetMarker("CustomerID", 11)
            );

            IDbDataParameter p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CompanyName", 1);
            p.DbType=DbType.String;
            p.Value=data.CompanyName;
            p.Size=40;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ContactName", 2);
            p.DbType=DbType.String;
            p.Value=(object)data.ContactName ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("ContactTitle", 3);
            p.DbType=DbType.String;
            p.Value=(object)data.ContactTitle ?? (object)DBNull.Value;
            p.Size=30;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Address", 4);
            p.DbType=DbType.String;
            p.Value=(object)data.Address ?? (object)DBNull.Value;
            p.Size=60;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("City", 5);
            p.DbType=DbType.String;
            p.Value=(object)data.City ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Region", 6);
            p.DbType=DbType.String;
            p.Value=(object)data.Region ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("PostalCode", 7);
            p.DbType=DbType.String;
            p.Value=(object)data.PostalCode ?? (object)DBNull.Value;
            p.Size=10;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Country", 8);
            p.DbType=DbType.String;
            p.Value=(object)data.Country ?? (object)DBNull.Value;
            p.Size=15;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Phone", 9);
            p.DbType=DbType.String;
            p.Value=(object)data.Phone ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("Fax", 10);
            p.DbType=DbType.String;
            p.Value=(object)data.Fax ?? (object)DBNull.Value;
            p.Size=24;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            p=command.CreateParameter();
            p.ParameterName=parameterBuilder.GetName("CustomerID", 11);
            p.DbType=DbType.String;
            p.Value=data.CustomerID;
            p.Size=5;
            p.Direction=ParameterDirection.Input;
            command.Parameters.Add(p);

            return command;
        }

        /// <summary>Gets the parameter builder instance.</summary>
        /// <value>The parameter builder instance.</value>
        protected ParameterBuilder ParameterBuilder
        {
            get
            {
                return _ParameterBuilder;
            }
        }

        private ParameterBuilder _ParameterBuilder;

    }

}
